//index.js

var app = getApp();
Page({
  data: {
    phone: '',
    password: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
  },

  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo;
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    });

    this.autoLogin();
  },

  autoLogin() {
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var input = {};
        input.code = res.code;

        wx.showLoading({
          title: '加载中...',
        });
        wx.request({
          url: app.globalData.serviceUrl + "/wcSysUser/getUserByWxLoginCode",
          method: "get",
          data: input,
          success: function (resp) {
            wx.hideLoading();
            app.globalData.userInfo = resp.data.data;
          },
          fail: function (error) {
            wx.hideLoading();
            wx.showToast({
              title: "通讯异常: " + JSON.stringify(error),
              icon: 'none',
            });
          }
        });
      }
    });

    wx.switchTab({
      url: "/pages/all/wan/wan1"
    });

  },

  setTimeoutJump() {
    let that = this;

    console.log("2秒后跳转到首页");
    if (that.data.hasUserInfo) {
      setTimeout(function () {
        that.autoLogin();

      }, 2000);

    } else {
      wx.showToast({
        title: "获取用户信息失败",
        icon: 'none',
      });
    }
  },

  onLoad: function (options) {
    // if (app.globalData.userInfo) {
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true,
    //   });

    //   _this.setTimeoutJump();
    // }
    // else if (this.data.canIUse) {
    //   app.userInfoReadyCallback = function (res) {
    //     _this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true,
    //     });

    //     _this.setTimeoutJump();
    //   };
    // }
    // else {
    //   wx.getUserInfo({
    //     success: function (res) {
    //       app.globalData.userInfo = res.userInfo;
    //       _this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true,
    //       });

    //       _this.setTimeoutJump();
    //     },
    //   });
    // }

    let that = this;
    setTimeout(function () {
      that.autoLogin();
    }, 2000);

  },

  // 获取输入账号
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  // 获取输入密码
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  //忘记密码
  wangji: function () {
    wx.navigateTo({
      // url: '../wangji/wangji',
    })
  },

  // 登录
  login: function () {
    if (this.data.phone.length == 0 || this.data.password.length == 0) {


      wx.showToast({
        title: '账号或密码错误！',
        icon: 'none',
        duration: 1000
      })

      return;
    }


    if (this.data.phone.length == 0) {


      wx.showToast({
        title: '账户不能为空！',
        icon: 'none',
        duration: 1000
      })

      return;
    }

    if (this.data.password.length == 0) {


      wx.showToast({
        title: '密码不能为空！',
        icon: 'none',
        duration: 1000
      })

      return;
    }



    {

      var that = this;
      wx.request({
        url: app.globalData.wancheURL + 'getUserUserAnPassLogin',
        data: {
          username: that.data.phone,
          pass: that.data.password
        },
        method: 'GET',

        success: function (res) {




          if (!res.data) {

            wx.showToast({
              title: '账户用户名密码错误！',
              icon: 'none',
              duration: 1000
            })
            console.log(res.data);
            console.log(res.data.forsysuser_id);
            return;

          } else {


            app.globalData.gluserName = res.data.mobile;//获取用户名编码既手机号
            app.globalData.glturstName = res.data.realName;//获取真实姓名
            console.log(app.globalData.gluserName + app.globalData.glturstName)
            app.globalData.glCode = res.data.code; //获取上一级的业务员编码
            app.globalData.forsysuser_id = res.data.forsysuser_id; //获取所属权限编码
            app.globalData.userid = res.data.id;//获取会员号


            console.log(app.globalData.gluserName + app.globalData.glturstName + app.globalData.glCode + app.globalData.forsysuser_id + app.globalData.userid);

            //登录成功后跳转到首页
            wx.switchTab({
              url: '/pages/all/wan/wan1'
            });


          }





          /*
                    if(res.data=='1'){
                    wx.navigateTo({
                      url: '/pages/register/team?code='+ that.data.phone
                    });
                     return;
                  }
          
                  if(res.data=='2'){
                    wx.navigateTo({
                      url: '/pages/order/order/order'
                    });
                     return;
          
          
                  }
                  */




        },
        fail: function () {
          // fail
        },
        complete: function () {

        }
      })



    }
  }
})
